package cn.bearspark.api.controller.main;

import cn.bearspark.api.service.main.DraftService;
import cn.bearspark.api.vo.main.draft.DraftSimpleVO;
import cn.bearspark.api.vo.main.draft.DraftVO;
import cn.bearspark.api.vo.main.draft.SaveDraftVO;
import cn.bearspark.common.dto.Page;
import cn.bearspark.framework.web.starter.limit.RequestLimit;
import cn.bearspark.framework.web.starter.vo.ResponseVO;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 专栏的草稿模块
 *
 * @author f
 */
@Validated
@RestController
@RequestMapping("/draft")
public class DraftController {

    private final DraftService draftService;

    public DraftController(DraftService draftService) {
        this.draftService = draftService;
    }

    /**
     * 保存草稿
     *
     * @param saveDraftVO 保存草稿的请求对象
     * @return 含有 草稿 id 的响应对象
     */
    @PostMapping
    public ResponseVO<Long> save(@Valid @RequestBody SaveDraftVO saveDraftVO) {
        return ResponseVO.success(draftService.save(saveDraftVO));
    }

    /**
     * 获取草稿
     * <p>
     * 每个用户每秒只能请求一次
     *
     * @param draftId 待获取的草稿 id
     * @return 草稿
     */
    @GetMapping("/{draftId}")
    @RequestLimit(limit = 1, second = 1)
    public ResponseVO<DraftVO> get(@PathVariable Long draftId) {
        return ResponseVO.success(draftService.get(draftId));
    }

    /**
     * 删除草稿
     *
     * @param draftId 待删除的草稿 id
     * @return 响应对象
     */
    @DeleteMapping("/{draftId}")
    public ResponseVO<Object> delete(@PathVariable Long draftId) {
        draftService.delete(draftId);
        return ResponseVO.success();
    }

    /**
     * 分页获取草稿列表
     * <p>
     * 按照最后保存时间倒序排序
     * <p>
     * 每个用户每秒只能请求一次
     *
     * @param pageNum  页数
     * @param pageSize 元素数
     * @return 草稿列表
     */
    @GetMapping("/{pageNum}/{pageSize}")
    @RequestLimit(limit = 1, second = 1)
    public ResponseVO<Page<DraftSimpleVO>> page(@PathVariable Integer pageNum,
                                                @PathVariable Integer pageSize) {
        return ResponseVO.success(draftService.page(pageNum, pageSize));
    }

}
